<template>
  <div class="container">
    <!-- 顶部按钮部分 -->
    <div class="options">
      <!-- 左侧操作部分 -->
      <div class="optionsLeft">
        <span class="className">关键字：</span>
        <el-input
          size="mini"
          placeholder="根据编号搜索"
          v-model="pageQuery.keyword"
        ></el-input>
      </div>
      <!-- 右侧操作部分 -->
      <div class="optionsRight">
        <el-button size="mini" @click="clear">清除</el-button>
        <el-button type="primary" size="mini" @click="onSearch">搜索</el-button>
      </div>
    </div>
    <!-- 中间数据条数部分 -->
    <div class="numberData">
      <i class="el-icon-warning"></i>
      <span>数据一共{{ count }}条</span>
    </div>
    <!-- 下方表格部分 -->
    <div class="table">
      <el-table
        highlight-current-row
        :header-cell-style="{
          background: '#fafafa',
          color: '#909399',
        }"
        :data="tableData"
      >
        <el-table-column
          label="编号"
          width="220"
          style="backgroundcolor: #1f1"
          prop="id"
        >
        </el-table-column>
        <el-table-column
          label="题型"
          prop="questionType"
          width="80"
        ></el-table-column>
        <el-table-column label="题目编号" width="200">
          <template v-slot="{ row }">
            <div v-for="(item, index) in row.questionIDs" :key="index">
              <a
                href="javascript:void(0)"
                @click="getPreview(item.id)"
                class="number-color"
                >{{ item.number }}</a
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" prop="addTime"> </el-table-column>
        <el-table-column label="答题时间(s)" prop="totalSeconds">
        </el-table-column>
        <el-table-column label="正确率(%)" prop="accuracyRate">
        </el-table-column>
        <el-table-column label="录入人" prop="userName" width="150">
        </el-table-column>
        <el-table-column property="address" label="操作" width="80">
          <template v-slot="{ row }">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="removeItem(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next,jumper"
      :total="count"
      :page-sizes="[20, 30, 50, 100]"
      :page-size="pageQuery.pagesize"
      :current-page="pageQuery.page"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
    </el-pagination>
    <questionsPreview
      :dialogVisible.sync="dialogVisible"
      :detailData="previewData"
      :previewData="previewData"
    ></questionsPreview>
  </div>
</template>

<script>
import { randoms as getRandoms, removeRandoms, detail as questionDetail } from '@/api/hmmm/questions.js'
import questionsPreview from '../components/questions-preview'
export default {
  data () {
    return {
      tableData: [],
      count: 0,
      dialogVisible: false,
      previewData: {},
      pageQuery: {
        pagesize: 20,
        page: 1,
        keyword: ''
      }
    }
  },
  components: {
    questionsPreview
  },
  created () {
    this.getRandomList()
  },
  methods: {
    async getRandomList () {
      const { data } = await getRandoms(this.pageQuery)
      this.tableData = data.items
      // 保留总数
      this.count = data.counts
      console.log(this.tableData, this.count)
    },
    handleCurrentChange (val) {
      this.pageQuery.page = val
      // 重新获取表格数据
      this.getRandomList()
    },
    handleSizeChange (val) {
      this.pageQuery.pagesize = val
      // 重新获取表格数据
      this.getRandomList()
    },
    // 删除
    removeItem (item) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async (res) => {
        if (res === 'confirm') {
          await removeRandoms(item)
          // 删除成功提示
          this.$message.success('删除成功')
          // 重新获取表格数据
          this.getRandomList()
        }
      })
    },
    // 清除
    clear () {
      this.pageQuery.keyword = ''
      this.pageQuery.page = 1
      this.getRandomList()
    },
    // 搜索
    onSearch () {
      this.pageQuery.page = 1
      this.getRandomList()
    },
    async getPreview (id) {
      try {
        const { data } = await questionDetail({ id })
        console.log(data)
        this.previewData = data
        this.dialogVisible = true
      } catch (err) {
        this.$message.info('未找到')
        return err
      }
    }
  }

}
</script>

<style  lang='scss'>
//
.container {
  margin: 20px 10px;
  background-color: #fff;
  padding: 20px;
  .options {
    width: 100%;
    height: 51px;
    display: flex;
    .optionsLeft {
      width: 75%;
      display: flex;
      .el-input {
        width: 200px;
        height: 32px;
        margin-right: 10px;
      }
      .el-button {
        width: 56px;
        height: 32px;
      }
      .className {
        font-size: 12px;
        width: 60px;
        line-height: 32px;
        margin-left: 15px;
      }
    }
    .optionsRight {
      display: flex;
      flex: 1;
      justify-content: flex-end;
      .el-button {
        width: 56px;
        height: 32px;
      }
    }
  }
  .numberData {
    width: 100%;
    padding: 8px 16px;
    margin-bottom: 20px;
    background-color: #f4f4f5;
    color: #909399;
    box-sizing: border-box;
    border-radius: 4px;
    position: relative;
    overflow: hidden;
    opacity: 1;
    display: flex;
    align-items: center;
    transition: opacity 0.2s;

    i {
      width: 16px;
      height: 16px;
    }

    span {
      margin-left: 5px;
      font-size: 12px;
    }
  }
  .number-color {
    color: rgb(0, 153, 255);
  }
}

.el-pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
